/*******************************************************************************
 * @project: aohuaproject
 * @package: com.aohua.px
 * @file: ApproverFromAIEventAction.java
 * @author: fionn
 * @created: 2015年11月24日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * 
 * Copyright 2015 HAND All rights reserved.
 ******************************************************************************/
package com.aohua.px;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.agile.api.APIException;
import com.agile.api.ChangeConstants;
import com.agile.api.IAgileSession;
import com.agile.api.ICell;
import com.agile.api.IChange;
import com.agile.api.IDataObject;
import com.agile.api.IItem;
import com.agile.api.INode;
import com.agile.api.ITable;
import com.agile.api.ITwoWayIterator;
import com.agile.api.IUser;
import com.agile.api.ItemConstants;
import com.agile.px.ActionResult;
import com.agile.px.EventActionResult;
import com.agile.px.ICustomAction;
import com.agile.px.IEventAction;
import com.agile.px.IEventInfo;
import com.agile.px.IWFChangeStatusEventInfo;
import com.hand.agile.utils.CellUtils;

/**
 * @author fionn
 *
 */
public class ApproverFromAIEventAction implements IEventAction, ICustomAction {

    /**
     * 
     */
    public ApproverFromAIEventAction() {
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see com.agile.px.IEventAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.px.IEventInfo)
     */
    @Override
    public EventActionResult doAction(IAgileSession session, INode arg1,
            IEventInfo arg2) {
        EventActionResult result = null;
        try {
            session.disableAllWarnings();            
            IWFChangeStatusEventInfo info = (IWFChangeStatusEventInfo)arg2;
            IChange change = (IChange)info.getDataObject();
            result = new EventActionResult(info, doAction(change));
        }catch (Exception e) {
            e.printStackTrace(System.out);
            result = new EventActionResult(arg2, new ActionResult(ActionResult.EXCEPTION, e));
        }finally {
            try {
                session.enableAllWarnings();
            } catch (APIException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /* (non-Javadoc)
     * @see com.agile.px.ICustomAction#doAction(com.agile.api.IAgileSession, com.agile.api.INode, com.agile.api.IDataObject)
     */
    @Override
    public ActionResult doAction(IAgileSession session, INode arg1,
            IDataObject arg2) {
        ActionResult result = null;
        try {
            session.disableAllWarnings();
            IChange change = (IChange)arg2;
            result = doAction(change);
        }catch (Exception e) {
            e.printStackTrace(System.out);
            result = new ActionResult(ActionResult.EXCEPTION, e);
        }finally {
            try {
                session.enableAllWarnings();
            } catch (APIException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    private ActionResult doAction(IChange change) throws APIException{
        ActionResult result = null;
        ITable aitable = change.getTable(ChangeConstants.TABLE_AFFECTEDITEMS);
        ITwoWayIterator it = aitable.getReferentIterator();
        IItem item = null;
        ICell cell = null;
        Set<IUser> users = new HashSet<IUser>();
        while(it.hasNext()) {
            item = (IItem)it.next();
            cell = item.getCell(ItemConstants.ATT_PAGE_TWO_CREATE_USER);
            users.add((IUser)CellUtils.getSingleListCellValue(cell));
        }
        change.addReviewers(change.getStatus(), users, Collections.EMPTY_SET, Collections.EMPTY_SET, false, "");
        result = new ActionResult(ActionResult.STRING, "添加受影响物件创建者作为流程审批者成功。");
        return result;
    }

}
